class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size();
        int left=0;
        int right=0;
        int mx = 0;
        int t_k = 0;
        while(right<n)
        {
            if(nums[right]==1)
            {
                right++;
            }
            else
            {
                if(t_k<k)
                {
                    right++;
                    t_k++;
                }
                else
                {
                    if(mx < (right-left))
                    {
                        mx = right-left;
                    }
                    
                   while(t_k>=k)
                   {
                       if(nums[left]==0)
                        {
                           t_k --; 
                        }
                       left++;
                   }
                }
            }
        }
        if(mx < (right-left))
        {
            mx = right-left;
        }
        return mx;
    }
};

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n=nums.size();
        int ret = 0;
        for(int left=0,right=0,count=0;right<n;right++)
        {
            if(nums[right]==0) count++;
            while(count>k)
            {
                if(nums[left++]==0) count--;
            }
            ret = max(ret,right-left+1);
        }
        return ret;
    }
};